// miniprogram/pages/home/home.js
const app = getApp()
var common = require('../../utils/common.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    year: 0,
    month: 0,
    inOutTotal: {},
    inOutDetail: [],
    deleteButtons: [{
      type: 'warn',
      text: '删除'
    }],
    preDeleteId:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.startPullDownRefresh()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this
    var currentDate = new Date()
    that.setData({
      year: currentDate.getFullYear(),
      month: currentDate.getMonth() + 1
    })
    //防止刚进入app，还未拿到userInfo，所以这里用回调，确保能获取数据
    if (app.globalData.userInfo) {
      that.getInOutTotal()
    } else {
      app.userInfoReadyCallback = userInfo => {
        //用户信息保存到全局
        app.globalData.userInfo = userInfo
        that.getInOutTotal()
      }
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  onPageScroll: function (e) {
    this.setData({
      scrollTop: e.scrollTop
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getInOutTotal: function () {
    var that = this
    wx.request({
      url: `${common.baseUrl()}/inOut/totalByMonth/${that.data.year}/${that.data.month}`,
      method: "GET",
      header: {
        // "Content-Type": "application/x-www-form-urlencoded",
        "Authorization": app.globalData.token
      },
      // data: {
      //   userId: app.globalData.userInfo.id
      // },
      success(res) {
        console.log(res)
        if (res.data.flag) {
          that.setData({
            inOutTotal: res.data.data
          })
          that.getInOutDetail()
        } else {
          wx.stopPullDownRefresh()
        }
      },
      fail(res) {
        wx.stopPullDownRefresh()
        console.log(res)
      }
    })
  },
  getInOutDetail: function () {
    var that = this
    wx.request({
      url: `${common.baseUrl()}/inOut/detailByMonth/${that.data.year}/${that.data.month}`,
      method: "GET",
      header: {
        // "Content-Type": "application/x-www-form-urlencoded",
        "Authorization": app.globalData.token
      },
      // data: {
      //   userId: app.globalData.userInfo.id
      // },
      success(res) {
        wx.stopPullDownRefresh()
        console.log(res)
        if (res.data.flag) {
          that.setData({
            inOutDetail: res.data.data
          })
        }
      },
      fail(res) {
        wx.stopPullDownRefresh()
        console.log(res)
      }
    })
  },
  addClick: function (e) {
    if(e.currentTarget.dataset.item){
      if(this.data.preDeleteId==e.currentTarget.dataset.item.id) return
    }
    wx.navigateTo({
      url: `createInOut/createInOut?detail=${encodeURIComponent(JSON.stringify(e.currentTarget.dataset.item))}`,
    })
  },
  slideShow: function (e) {
    console.log(e.currentTarget.dataset.id)
    this.setData({
      preDeleteId:e.currentTarget.dataset.id
    })
  },
  expendSlideClick: function (e) {
    var that = this
    wx.showLoading({
      title: '正在删除',
    })
    wx.request({
      url: `${common.baseUrl()}/expend/remove/${e.currentTarget.dataset.id}`,
      method: "DELETE",
      header: {
        // "Content-Type": "application/x-www-form-urlencoded",
        "Authorization": app.globalData.token
      },
      // data: {
      //   userId: app.globalData.userInfo.id
      // },
      success(res) {
        wx.hideLoading()
        console.log(res)
        if (res.data.flag) {
          //刷新列表
          wx.showToast({
            title: res.data.message
          })
          wx.startPullDownRefresh()
        }else{
          wx.showToast({
            title: res.data.message,
            image: '/images/warning.png'
          })
        }
      },
      fail(res) {
        console.log(res)
      }
    })
  },
  incomeSlideClick: function (e) {
    var that = this
    wx.showLoading({
      title: '正在删除',
    })
    wx.request({
      url: `${common.baseUrl()}/income/remove/${e.currentTarget.dataset.id}`,
      method: "DELETE",
      header: {
        // "Content-Type": "application/x-www-form-urlencoded",
        "Authorization": app.globalData.token
      },
      // data: {
      //   userId: app.globalData.userInfo.id
      // },
      success(res) {
        wx.hideLoading()
        console.log(res)
        if (res.data.flag) {
          //刷新列表
          wx.showToast({
            title: res.data.message
          })
          wx.startPullDownRefresh()
        }else{
          wx.showToast({
            title: res.data.message,
            image: '/images/warning.png'
          })
        }
      },
      fail(res) {
        console.log(res)
      }
    })
  }
})